package com.sanpu.kxe.business.repay.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.repay.adapter.QueryPlanAdapter;
import com.sanpu.kxe.business.repay.bean.QueryPlanOutput;
import com.sanpu.kxe.business.repay.contract.QueryPlanNewContract;
import com.sanpu.kxe.business.repay.contract.RepayConfig;
import com.sanpu.kxe.business.repay.presenter.QueryPlanNewPresenter;
import com.sanpu.kxe.business.repay.provider.RepayOpenProvider;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.jump.eventbus.RefreshCancelPlanEvent;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.sanpu.kxe.R.id.swipeRefreshLayout;

/**
 * Description：查询计划
 * Created by 薛勇军 on 2019/5/28.
 */

public class QueryPlanNewActivity extends BaseTitleActivity implements QueryPlanNewContract.View {

    @Bind(R.id.allTv)
    TextView allTv;
    @Bind(R.id.successTv)
    TextView successTv;
    @Bind(R.id.procesingTv)
    TextView procesingTv;
    @Bind(R.id.failTv)
    TextView failTv;
    @Bind(R.id.compensateTv)
    TextView compensateTv;

    @Bind(swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @Bind(R.id.queryPlanRv)
    RecyclerView queryPlanRv;
    @Bind(R.id.emptyLl)
    LinearLayout emptyLl;

    private QueryPlanAdapter queryPlanAdapter;

    private QueryPlanNewContract.Presenter mPresenter;

    private String mCurrentStatus = "ALL";

    private int nowPage = 0;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_query_plan_new;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("我的计划");

        String mercCardId = getIntent().getStringExtra(RepayConfig.INTENT_MERCCARDID);

        queryPlanRv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        queryPlanAdapter = new QueryPlanAdapter();
        queryPlanRv.setAdapter(queryPlanAdapter);
        queryPlanAdapter.disableLoadMoreIfNotFullPage(queryPlanRv);

        mPresenter = new QueryPlanNewPresenter(this, mercCardId);
        mPresenter.cardPlanList(nowPage, mCurrentStatus);
    }

    @OnClick({R.id.allTv, R.id.successTv, R.id.procesingTv, R.id.failTv, R.id.compensateTv})
    public void onClick(View view) {
        String chooseStatus = null;
        switch (view.getId()) {
            case R.id.allTv:
                chooseStatus = "ALL";
                break;
            case R.id.successTv:
                chooseStatus = "SUCCESS";
                break;
            case R.id.procesingTv:
                chooseStatus = "PROCESING";
                break;
            case R.id.failTv:
                chooseStatus = "FAIL";
                break;
            case R.id.compensateTv:
                chooseStatus = "COMPENSATE";
                break;
        }
        if (TextUtils.isEmpty(mCurrentStatus) && TextUtils.isEmpty(chooseStatus)) {
            return;
        }
        if (!TextUtils.isEmpty(mCurrentStatus) && TextUtils.equals(mCurrentStatus, chooseStatus)) {
            return;
        }
        mCurrentStatus = chooseStatus;
        initBarView();
        nowPage = 0;
        mPresenter.cardPlanList(nowPage, mCurrentStatus);
    }

    @Override
    protected void setListener() {
        super.setListener();
        queryPlanAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.takeDetailsRtv) {
                    QueryPlanOutput queryPlan = (QueryPlanOutput) adapter.getItem(position);
                    if (queryPlan != null) {
                        RepayOpenProvider.getInstance().openQueryPlanDetailsActivity(mContext,
                                queryPlan.getRepayOrderNo(), queryPlan.getPlanStatus(),
                                queryPlan.getTradeTime());
                    }
                }
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = 0;
                mPresenter.cardPlanList(nowPage, mCurrentStatus);
                queryPlanAdapter.setEnableLoadMore(true);
            }
        });
        queryPlanAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.cardPlanList(nowPage, mCurrentStatus);
            }
        }, queryPlanRv);
        queryPlanAdapter.setPreLoadNumber(2);
    }

    /**
     * tab状态初始化
     */
    private void initBarView() {
        allTv.setBackground(null);
        allTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
        allTv.setTextSize(13);
        successTv.setBackground(null);
        successTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
        successTv.setTextSize(13);
        procesingTv.setBackground(null);
        procesingTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
        procesingTv.setTextSize(13);
        failTv.setBackground(null);
        failTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
        failTv.setTextSize(13);
        compensateTv.setBackground(null);
        compensateTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
        compensateTv.setTextSize(13);

        if (!TextUtils.isEmpty(mCurrentStatus)) {
            switch (mCurrentStatus) {
                case "ALL":
                    allTv.setBackgroundResource(R.drawable.shape_bar_bottom_line);
                    allTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
                    allTv.setTextSize(15);
                    break;
                case "SUCCESS":
                    successTv.setBackgroundResource(R.drawable.shape_bar_bottom_line);
                    successTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
                    successTv.setTextSize(15);
                    break;
                case "PROCESING":
                    procesingTv.setBackgroundResource(R.drawable.shape_bar_bottom_line);
                    procesingTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
                    procesingTv.setTextSize(15);
                    break;
                case "FAIL":
                    failTv.setBackgroundResource(R.drawable.shape_bar_bottom_line);
                    failTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
                    failTv.setTextSize(15);
                    break;
                case "COMPENSATE":
                    compensateTv.setBackgroundResource(R.drawable.shape_bar_bottom_line);
                    compensateTv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
                    compensateTv.setTextSize(15);
                    break;
            }
        }
    }

    @Override
    public void showDataView(List<QueryPlanOutput> planOutputs, boolean isFirst) {
        if (planOutputs != null && planOutputs.size() > 0) {
            if (isFirst) {
                queryPlanAdapter.setNewData(planOutputs);
            } else {
                queryPlanAdapter.addData(planOutputs);
            }
            nowPage++;
            vSwipeRefreshLayout.setVisibility(View.VISIBLE);
            emptyLl.setVisibility(View.GONE);
        } else if (isFirst) {
            vSwipeRefreshLayout.setVisibility(View.GONE);
            emptyLl.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                queryPlanAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                queryPlanAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                queryPlanAdapter.loadMoreFail();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(RefreshCancelPlanEvent event) {
        if (event != null) {
            nowPage = 0;
            mPresenter.cardPlanList(nowPage, mCurrentStatus);
            queryPlanAdapter.setEnableLoadMore(true);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(QueryPlanNewContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
